﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockSBIE;
using System.Data.SQLite;
using System.Data.Common;
namespace StockSBDetails
{
    public partial class InstManagerForm : Form
    {
        public InstManagerForm()
        {
            InitializeComponent();
        }

        protected void updateInstCheckBox(String filter)
        {
            String sql = "SELECT  InstCode,InstName, InstGroupId FROM      Insts  WHERE   (InstGroupId IS NULL) ORDER BY InstCode";
            DbCommand cmd =  SQLFactory.getInstance().getCommand(sql);
            DbDataReader rdr = cmd.ExecuteReader();
            String name = "";
            instCheckedListBox1.Items.Clear();
            List<String> names = new List<string>();
            while (rdr.Read())
            {
                if (filter != "")
                {
                    name = rdr.GetString(1);
                    if (name.StartsWith(filter))
                    {
                        name = rdr.GetString(0) + "  " + rdr.GetString(1);
                        names.Add(name);
                    }
                }
                else
                {
                    name = rdr.GetString(0) + "  " + rdr.GetString(1);
                    names.Add(name);
                }
            }
            instCheckedListBox1.Items.AddRange(names.ToArray());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            updateInstCheckBox("");            
        }

        private void instCheckedListBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int indexitem=instCheckedListBox1.IndexFromPoint(e.X, e.Y);
            if ((indexitem >= 0) && (indexitem < instCheckedListBox1.Items.Count))
            {
                instCheckedListBox1.DoDragDrop(instCheckedListBox1.Items[indexitem], DragDropEffects.Copy);
            }
        }

        private void InstNameTextBox2_DragDrop(object sender, DragEventArgs e)
        {


        }

        private void instCheckedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            updateInstCheckBox(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sz=instCheckedListBox1.Items.Count;
            for(int i=0;i<sz;i++)
            {
                instCheckedListBox1.SetItemChecked(i, true);
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int id = instCheckedListBox1.SelectedIndex;
            if (id < 0)
                return;
            String item = (String)instCheckedListBox1.Items[id];
            int pos = item.IndexOf(" "); ;
            String s1 = item.Substring(0, pos).Trim();
            String s2=item.Substring(pos + 1).Trim();
            igtextBox3.Text = s1;
            igtextBox4.Text = s2;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Guid groupId = StockSBDetailManager.createInstGroup(igtextBox3.Text,igtextBox4.Text);
            int sz = instCheckedListBox1.Items.Count;
            for (int i = 0; i < sz; i++)
            {
                if (instCheckedListBox1.GetItemChecked(i))
                {
                    String item = (String)instCheckedListBox1.Items[i];
                    int pos = item.IndexOf(" "); ;
                    String s1 = item.Substring(0, pos).Trim();
                    StockSBDetailManager.setGroupInstId(s1, groupId);
                }
            }


        }
    }
}
